package com.example.workassignback.serviceImpl;

import com.example.workassignback.Mapper.PatientMapper;
import com.example.workassignback.POJO.Patient;
import com.example.workassignback.POJO.Process;
import com.example.workassignback.POJO.ProcessItem;
import com.example.workassignback.serviceImpl.service.PatientService;
import com.example.workassignback.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PatientServiceImpl implements PatientService {
    @Autowired
    private PatientMapper patientMapper;


    @Override
    public Result patientReport(Integer id) {
        LocalDate currentDate = LocalDate.now();
        Integer pId = patientMapper.getPIdByUId(id);
        if (patientMapper.isPatientReport(pId, currentDate) == null) {
            return Result.fail("今日没有待报到的流程");
        } else if (patientMapper.isPatientReport(pId, currentDate)) {
            return Result.fail("今日您已报到");
        }else {
            System.out.println("PatientServiceImpl:" + pId);
            patientMapper.patientReport(pId);
            return Result.success("报到成功");
        }
    }

    @Override
    public Result patientGetProcess(Integer id) {
        LocalDate currentDate = LocalDate.now();
        Integer pId = patientMapper.getPIdByUId(id);
        List<ProcessItem> currentProcess = patientMapper.getPatientProcessByDate(pId, currentDate);

        Map map = new HashMap<>();
        map.put("Process", currentProcess);

        return Result.success(map);
    }

    @Override
    public Result patientGetHistory(Integer id) {
        Integer pId = patientMapper.getPIdByUId(id);
        List<ProcessItem> HistoryProcessList = patientMapper.getPatientHistoryProcessInfo(pId);
        List<Integer> HistoryPrIdList = patientMapper.getPatientHistoryPrId(pId);

        Map map = new HashMap<>();
        map.put("PrIdList", HistoryPrIdList);
        map.put("ProcessList", HistoryProcessList);

        return Result.success(map);
    }

    @Override
    public Result patientGetHistoryByDate(Integer id, LocalDateTime startTime) {
        Integer pId = patientMapper.getPIdByUId(id);
        LocalDate historyDate = startTime.toLocalDate();
        List<ProcessItem> histroyProcess = patientMapper.getPatientProcessByDate(pId,historyDate);

        Map map = new HashMap<>();
        map.put("Process", histroyProcess);

        return Result.success(map);
    }



    @Override
    public Result patientGetInfo(Integer id) {
        Integer pId = patientMapper.getPIdByUId(id);
        Patient patient = patientMapper.getInfoByUId(id);

        Map map = new HashMap<>();
        map.put("Info", patient);

        return Result.success(map);
    }

    @Override
    public Result patientModifyInfo(Patient patient) {
        System.out.println(patient);
        if (patientMapper.patientModifyUserInfo(patient) && patientMapper.patientModifyPatientInfo(patient)) {
            return Result.success("成功修改");
        }else{
            return Result.fail("修改失败");
        }
    }

}
